// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.erosion.categories;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import zombie.core.Rand;
import zombie.erosion.ErosionData;
import zombie.erosion.obj.ErosionObj;
import zombie.erosion.obj.ErosionObjSprites;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.sprite.IsoSprite;

public final class NaturePlants extends ErosionCategory {
    private final int[][] soilRef = new int[][]{
        {17, 17, 17, 17, 17, 17, 17, 17, 17, 1, 2, 8, 8},
        {11, 12, 1, 2, 8, 1, 2, 8, 1, 2, 8, 1, 2, 8, 1, 2, 8},
        {11, 12, 11, 12, 11, 12, 11, 12, 15, 16, 18, 19},
        {22, 22, 22, 22, 22, 22, 22, 22, 22, 3, 4, 14},
        {15, 16, 3, 4, 14, 3, 4, 14, 3, 4, 14, 3, 4, 14},
        {11, 12, 15, 16, 15, 16, 15, 16, 15, 16, 21},
        {13, 13, 13, 13, 13, 13, 13, 13, 13, 5, 6, 24},
        {18, 19, 5, 6, 24, 5, 6, 24, 5, 6, 24, 5, 6, 24},
        {18, 19, 18, 19, 18, 19, 18, 19, 20, 21},
        {7, 7, 7, 7, 7, 7, 7, 7, 7, 9, 10, 23},
        {19, 20, 9, 10, 23, 9, 10, 23, 9, 10, 23, 9, 10, 23},
        {15, 16, 18, 19, 20, 19, 20, 19, 20}
    };
    private int[] spawnChance = new int[100];
    private ArrayList<ErosionObj> objs = new ArrayList();
    private final NaturePlants.PlantInit[] plants = new NaturePlants.PlantInit[]{
        new NaturePlants.PlantInit("Butterfly Weed", true, 0.05F, 0.25F),
        new NaturePlants.PlantInit("Butterfly Weed", true, 0.05F, 0.25F),
        new NaturePlants.PlantInit("Swamp Sunflower", true, 0.2F, 0.45F),
        new NaturePlants.PlantInit("Swamp Sunflower", true, 0.2F, 0.45F),
        new NaturePlants.PlantInit("Purple Coneflower", true, 0.1F, 0.35F),
        new NaturePlants.PlantInit("Purple Coneflower", true, 0.1F, 0.35F),
        new NaturePlants.PlantInit("Joe-Pye Weed", true, 0.8F, 1.0F),
        new NaturePlants.PlantInit("Blazing Star", true, 0.25F, 0.65F),
        new NaturePlants.PlantInit("Wild Bergamot", true, 0.45F, 0.6F),
        new NaturePlants.PlantInit("Wild Bergamot", true, 0.45F, 0.6F),
        new NaturePlants.PlantInit("White Beard-tongue", true, 0.2F, 0.65F),
        new NaturePlants.PlantInit("White Beard-tongue", true, 0.2F, 0.65F),
        new NaturePlants.PlantInit("Ironweed", true, 0.75F, 0.85F),
        new NaturePlants.PlantInit("White Baneberry", true, 0.4F, 0.8F),
        new NaturePlants.PlantInit("Wild Columbine", true, 0.85F, 1.0F),
        new NaturePlants.PlantInit("Wild Columbine", true, 0.85F, 1.0F),
        new NaturePlants.PlantInit("Jack-in-the-pulpit", false, 0.0F, 0.0F),
        new NaturePlants.PlantInit("Wild Ginger", true, 0.1F, 0.9F),
        new NaturePlants.PlantInit("Wild Ginger", true, 0.1F, 0.9F),
        new NaturePlants.PlantInit("Wild Geranium", true, 0.65F, 0.9F),
        new NaturePlants.PlantInit("Alumroot", true, 0.35F, 0.75F),
        new NaturePlants.PlantInit("Wild Blue Phlox", true, 0.15F, 0.55F),
        new NaturePlants.PlantInit("Polemonium Reptans", true, 0.4F, 0.6F),
        new NaturePlants.PlantInit("Foamflower", true, 0.45F, 1.0F)
    };

    @Override
    public boolean replaceExistingObject(
        IsoGridSquare _sq, ErosionData.Square _sqErosionData, ErosionData.Chunk _chunkData, boolean _isExterior, boolean _hasWall
    ) {
        int int0 = _sq.getObjects().size();

        for (int int1 = int0 - 1; int1 >= 1; int1--) {
            IsoObject isoObject = _sq.getObjects().get(int1);
            IsoSprite isoSprite = isoObject.getSprite();
            if (isoSprite != null && isoSprite.getName() != null) {
                if (isoSprite.getName().startsWith("d_plants_1_")) {
                    int int2 = Integer.parseInt(isoSprite.getName().replace("d_plants_1_", ""));
                    NaturePlants.CategoryData categoryData0 = (NaturePlants.CategoryData)this.setCatModData(_sqErosionData);
                    categoryData0.gameObj = int2 < 32 ? int2 % 8 : (int2 < 48 ? int2 % 8 + 8 : int2 % 8 + 16);
                    categoryData0.stage = 0;
                    categoryData0.spawnTime = 0;
                    _sq.RemoveTileObjectErosionNoRecalc(isoObject);
                    return true;
                }

                if (!"vegetation_groundcover_01_16".equals(isoSprite.getName()) && !"vegetation_groundcover_01_17".equals(isoSprite.getName())) {
                    if (!"vegetation_groundcover_01_18".equals(isoSprite.getName())
                        && !"vegetation_groundcover_01_19".equals(isoSprite.getName())
                        && !"vegetation_groundcover_01_20".equals(isoSprite.getName())
                        && !"vegetation_groundcover_01_21".equals(isoSprite.getName())
                        && !"vegetation_groundcover_01_22".equals(isoSprite.getName())
                        && !"vegetation_groundcover_01_23".equals(isoSprite.getName())) {
                        continue;
                    }

                    NaturePlants.CategoryData categoryData1 = (NaturePlants.CategoryData)this.setCatModData(_sqErosionData);
                    categoryData1.gameObj = Rand.Next(this.plants.length);
                    categoryData1.stage = 0;
                    categoryData1.spawnTime = 0;
                    _sq.RemoveTileObjectErosionNoRecalc(isoObject);

                    while (--int1 > 0) {
                        isoObject = _sq.getObjects().get(int1);
                        isoSprite = isoObject.getSprite();
                        if (isoSprite != null && isoSprite.getName() != null && isoSprite.getName().startsWith("vegetation_groundcover_01_")) {
                            _sq.RemoveTileObjectErosionNoRecalc(isoObject);
                        }
                    }

                    return true;
                }

                NaturePlants.CategoryData categoryData2 = (NaturePlants.CategoryData)this.setCatModData(_sqErosionData);
                categoryData2.gameObj = 21;
                categoryData2.stage = 0;
                categoryData2.spawnTime = 0;
                _sq.RemoveTileObjectErosionNoRecalc(isoObject);

                while (--int1 > 0) {
                    isoObject = _sq.getObjects().get(int1);
                    isoSprite = isoObject.getSprite();
                    if (isoSprite != null && isoSprite.getName() != null && isoSprite.getName().startsWith("vegetation_groundcover_01_")) {
                        _sq.RemoveTileObjectErosionNoRecalc(isoObject);
                    }
                }

                return true;
            }
        }

        return false;
    }

    @Override
    public boolean validateSpawn(
        IsoGridSquare _sq, ErosionData.Square _sqErosionData, ErosionData.Chunk _chunkData, boolean _isExterior, boolean _hasWall, boolean _isRespawn
    ) {
        if (_sq.getObjects().size() > (_hasWall ? 2 : 1)) {
            return false;
        } else if (_sqErosionData.soil >= 0 && _sqErosionData.soil < this.soilRef.length) {
            int[] int0 = this.soilRef[_sqErosionData.soil];
            int int1 = _sqErosionData.noiseMainInt;
            if (_sqErosionData.rand(_sq.x, _sq.y, 101) < this.spawnChance[int1]) {
                NaturePlants.CategoryData categoryData = (NaturePlants.CategoryData)this.setCatModData(_sqErosionData);
                categoryData.gameObj = int0[_sqErosionData.rand(_sq.x, _sq.y, int0.length)] - 1;
                categoryData.stage = 0;
                categoryData.spawnTime = 100 - int1;
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public void update(IsoGridSquare _sq, ErosionData.Square _sqErosionData, ErosionCategory.Data _sqCategoryData, ErosionData.Chunk _chunkData, int _eTick) {
        NaturePlants.CategoryData categoryData = (NaturePlants.CategoryData)_sqCategoryData;
        if (_eTick >= categoryData.spawnTime && !categoryData.doNothing) {
            if (categoryData.gameObj >= 0 && categoryData.gameObj < this.objs.size()) {
                ErosionObj erosionObj = (ErosionObj)this.objs.get(categoryData.gameObj);
                boolean boolean0 = false;
                byte _byte = 0;
                int _int = this.currentSeason(_sqErosionData.magicNum, erosionObj);
                boolean boolean1 = this.currentBloom(_sqErosionData.magicNum, erosionObj);
                this.updateObj(_sqErosionData, _sqCategoryData, _sq, erosionObj, boolean0, _byte, _int, boolean1);
            } else {
                this.clearCatModData(_sqErosionData);
            }
        }
    }

    @Override
    public void init() {
        for (int int0 = 0; int0 < 100; int0++) {
            if (int0 >= 20 && int0 < 50) {
                this.spawnChance[int0] = (int)this.clerp((float)(int0 - 20) / 30.0F, 0.0F, 8.0F);
            } else if (int0 >= 50 && int0 < 80) {
                this.spawnChance[int0] = (int)this.clerp((float)(int0 - 50) / 30.0F, 8.0F, 0.0F);
            }
        }

        this.seasonDisp[5].season1 = 0;
        this.seasonDisp[5].season2 = 0;
        this.seasonDisp[5].split = false;
        this.seasonDisp[1].season1 = 1;
        this.seasonDisp[1].season2 = 0;
        this.seasonDisp[1].split = false;
        this.seasonDisp[2].season1 = 2;
        this.seasonDisp[2].season2 = 2;
        this.seasonDisp[2].split = true;
        this.seasonDisp[4].season1 = 4;
        this.seasonDisp[4].season2 = 0;
        this.seasonDisp[4].split = true;
        String string = "d_plants_1_";
        ArrayList arrayList0 = new ArrayList();

        for (int int1 = 0; int1 <= 7; int1++) {
            arrayList0.add(string + int1);
        }

        ArrayList arrayList1 = new ArrayList();

        for (int int2 = 8; int2 <= 15; int2++) {
            arrayList1.add(string + int2);
        }

        byte _byte = 16;

        for (int int3 = 0; int3 < this.plants.length; int3++) {
            if (int3 >= 8) {
                _byte = 24;
            }

            if (int3 >= 16) {
                _byte = 32;
            }

            NaturePlants.PlantInit plantInit = this.plants[int3];
            ErosionObjSprites erosionObjSprites = new ErosionObjSprites(1, plantInit.name, false, plantInit.hasFlower, false);
            erosionObjSprites.setBase(0, arrayList0, 1);
            erosionObjSprites.setBase(0, arrayList1, 4);
            erosionObjSprites.setBase(0, string + (_byte + int3), 2);
            erosionObjSprites.setFlower(0, string + (_byte + int3 + 8));
            float float0 = plantInit.hasFlower ? plantInit.bloomstart : 0.0F;
            float float1 = plantInit.hasFlower ? plantInit.bloomend : 0.0F;
            ErosionObj erosionObj = new ErosionObj(erosionObjSprites, 30, float0, float1, false);
            this.objs.add(erosionObj);
        }
    }

    @Override
    protected ErosionCategory.Data allocData() {
        return new NaturePlants.CategoryData();
    }

    @Override
    public void getObjectNames(ArrayList<String> list) {
        for (int _int = 0; _int < this.objs.size(); _int++) {
            if (((ErosionObj)this.objs.get(_int)).name != null && !list.contains(((ErosionObj)this.objs.get(_int)).name)) {
                list.add(((ErosionObj)this.objs.get(_int)).name);
            }
        }
    }

    private static final class CategoryData extends ErosionCategory.Data {
        public int gameObj;
        public int spawnTime;

        @Override
        public void save(ByteBuffer byteBuffer) {
            super.save(byteBuffer);
            byteBuffer.put((byte)this.gameObj);
            byteBuffer.putShort((short)this.spawnTime);
        }

        @Override
        public void load(ByteBuffer byteBuffer, int _int) {
            super.load(byteBuffer, _int);
            this.gameObj = byteBuffer.get();
            this.spawnTime = byteBuffer.getShort();
        }
    }

    private class PlantInit {
        public String name;
        public boolean hasFlower;
        public float bloomstart;
        public float bloomend;

        public PlantInit(String string, boolean _boolean, float float0, float float1) {
            this.name = string;
            this.hasFlower = _boolean;
            this.bloomstart = float0;
            this.bloomend = float1;
        }
    }
}
